home *** CD-ROM | disk | FTP | other *** search
- #ifndef __MiscSparseSet_h
- #define __MiscSparseSet_h
- #ifdef __GNUC__
- # pragma interface
- #endif
- //=============================================================================
- //
- // Copyright (C) 1995 by Paul S. McCarthy and Eric Sunshine.
- // Written by Paul S. McCarthy and Eric Sunshine.
- // All Rights Reserved.
- //
- // This notice may not be removed from this source code.
- //
- // This object is included in the MiscKit by permission from the authors
- // and its use is governed by the MiscKit license, found in the file
- // "License.rtf" in the MiscKit distribution. Please refer to that file
- // for a list of all applicable permissions and restrictions.
- //
- //=============================================================================
- //-----------------------------------------------------------------------------
- // MiscSparseSet.h
- //
- // This object impelements a sparse set. The set is represented by an
- // array of ranges kept in sorted ascending order.
- //
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // $Id: MiscSparseSet.h,v 1.1 95/09/27 12:21:21 zarnuk Exp $
- // $Log: MiscSparseSet.h,v $
- // Revision 1.1 95/09/27 12:21:21 zarnuk
- // Initial revision
- //
- //-----------------------------------------------------------------------------
- #include "bool.h"
-
- class MiscSparseSet
- {
- public:
- int const INVALID = -1;
-
- private:
- struct Range
- {
- int min_val;
- int max_val;
- };
-
- unsigned int lim;
- unsigned int capacity;
- Range* array;
- int cursor;
-
- void expand( unsigned int new_capacity );
- void expand();
- int bsearch( int x ) const;
- void fixCursor();
- void insertAt( unsigned int, unsigned int lo, unsigned int hi );
- void deleteAt( unsigned int, unsigned int slots );
- int merge( int x, unsigned int at );
- int slice( int x, unsigned int at );
-
- public:
- MiscSparseSet() : lim(0), capacity(0), array(0), cursor(INVALID) {}
- MiscSparseSet( MiscSparseSet const& );
- ~MiscSparseSet();
-
- MiscSparseSet& operator=( MiscSparseSet const& );
- bool operator==( MiscSparseSet const& ) const;
- bool operator!=( MiscSparseSet const& s ) const
- { return !operator==(s); }
-
- int getCursor() const { return cursor; }
- void setCursor( int c ) { cursor = c; fixCursor(); }
- void clearCursor() { cursor = INVALID; }
-
- bool contains( int x ) const { return (bsearch( x ) >= 0); }
- bool isEmpty() const { return (lim == 0); }
- void empty() { lim = 0; clearCursor(); }
- unsigned int count() const; // # elments in set
-
- void add( int low, int high ); // add a range
- void add( int x ) { add( x, x ); }
- void remove( int low, int high ); // remove a range
- void remove( int x ) { remove( x, x ); }
- void toggle( int );
- void shiftUpAt( int x );
- void shiftDownAt( int x );
-
- unsigned int numRanges() const { return lim; }
- void getRangeAt( unsigned int i, int& lo, int& hi ) const;
- void getTotalRange( int& lo, int& hi ) const;
- };
-
- #endif // __MiscSparseSet_h
-